﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ether.Libs;
using Jitter;

namespace Ether.Mapping
{
    public class LevelMap
    {
        //Fields
        private List<TierMap> tierMapData;

        public World ParentWorld;
        public TierMap this[int level]
        {
            get { return tierMapData[level]; }
        }
        public int Tiers { get { return tierMapData.Count - 1; } }
        public int HighestLevelToDraw { get; private set; }

        public bool InvalidateSelectionCells { get; internal set; }

        //Construct
        public LevelMap(World parentWorld)
        {
            ParentWorld = parentWorld;
            tierMapData = new List<TierMap>();
            InvalidateSelectionCells = false;
        }
        
        //Methods
        public void AddTier(int[,] data)
        {
            AddTier(data, CellTextureType.Grass);
        }
        public void AddTier(int[,] data, CellTextureType Default)
        {
            TierMap tMap  = new TierMap(this, ParentWorld, Default, tierMapData.Count, data);

            tierMapData.Add(tMap);
            HighestLevelToDraw = tierMapData.Count - 1;
        }

        /// <summary>
        /// Attempts to hide the current Top tier (NOTE: Bottom Layer always shown)
        /// </summary>
        public void HideTopTier()
        {
            if (HighestLevelToDraw > 0) {
                InvalidateSelectionCells = true;
                HighestLevelToDraw--;
            }
        }
        /// <summary>
        /// Attempts to render another tier
        /// </summary>
        public void ShowAnotherTier()
        {
            if (HighestLevelToDraw < Tiers)
            {
                InvalidateSelectionCells = true;
                HighestLevelToDraw++;
            }
        }

        public void DebugDrawLevelMap(DebugDrawer dDrawer)
        {
            for (int i = 0; i <= HighestLevelToDraw; i++)
                this[i].DebugDrawTier(dDrawer); 
        }
        public void DrawLevelMap(Camera camera, GameTime time)
        {
            for(int i=0; i <= HighestLevelToDraw; i++)
                this[i].DrawTier(camera, time); 
        }
        public void UpdateLevelMap(GameTime time)
        {
            if (InvalidateSelectionCells) {
                SelectionEngine.Clear();
                SelectionEngine.Register(this.GetCells());
                InvalidateSelectionCells = false;
            }

            //The Tiers are always updated regardless
            foreach (TierMap map in tierMapData)
                map.UpdateTier(time);
        }

        public void BuildModeHilight()
        {
            for (int i = 0; i <= Tiers; i++) 
                this[i].BuildModeHilight(); 
        }
        public void ClearBuildModeHilighting()
        {
            for (int i = 0; i <= Tiers; i++)
                this[i].ClearBuildModeHilighting(); 
        }

        public ISelectable[] GetCells()
        {
            List<ISelectable> temp = new List<ISelectable>();

            for (int i = Tiers; i >= 0; i--)                
            {
                if (i <= HighestLevelToDraw) {
                    ISelectable[] teirCells = this[i].GetCells();
                    foreach (ISelectable cell in teirCells)
                        temp.Add(cell);
                }
            }

            return temp.ToArray();
        }

        
    }
}
